<?php
/**
 * 前台公共类，做基础服务
 * 用户检查，权限检查，获取用户信息
 * @datetime 2016-06-03
 * @author xiaojing
 */
namespace Common\Controller;
use Think\Controller;
class HomeController extends Controller
{
    /**
     * 初始化 前台 数据
     * @author xiaojing
     */
    public function _initialize()
    {
        $module     = strtolower(MODULE_NAME); //获取模块名

        $controller = strtolower(CONTROLLER_NAME); //获取控制器名

        $action     = strtolower(ACTION_NAME); //获取方法名

        /*读取站点配置*/
        $config = api('Config/lists');

        C($config); //添加配置

        if (!C('WEB_SITE_CLOSE')) {
            $this->error(L('_ERROR_WEBSITE_CLOSED_'));
        }

        $this->assign('islogin',is_login());

        $this->assign('action',$action);

    }

    /**
     * 获取用户数据
     * @return mixed array  用户信息
     * @author xiaojing
     */
    public function userInfo(){
        $uid = query_user(array( 'uid'));
        $user_info = D('UcenterMember')->where('status=1 and id='.$uid['uid'])->find();
        return  $user_info;
    }

    /**
     * 数组分页
     * @param $dataArr array 数组数据
     * @param $url string    链接地址
     * @param $totalRows     总的记录数
     * @param $listRows      每页显示记录数
     * @param $pages int     当前页
     * @return array
     * @author xiaojing
     */
    public function pageSet($dataArr,$url,$totalRows, $listRows, $pages=1) {

        $rollPage   = 5;// 分页栏每页显示的页数

        $nowPage = $pages;

        $firstRow   = $listRows * ($pages - 1);

        $datalist = array_slice($dataArr,$firstRow,$listRows);

        /* 计算分页信息 */
        $totalPages = ceil($totalRows / $listRows); //总页数

        if(!empty($totalPages) && $nowPage > $totalPages) {
            $nowPage = $totalPages;
        }

        /* 计算分页零时变量 */
        $now_cool_page      = $rollPage/2;

        $now_cool_page_ceil = ceil($now_cool_page);
        $up_row             = $nowPage - 1;//上一页
        $up_page            = $up_row > 0 ?
                                '<li class="paginate_button " aria-controls="dynamic-table" tabindex="0"><a href="javascript:void(0)" hrefs="'.$url.'" page="'.$up_row.'">&lt;</a></li>' :
                                '<li class="paginate_button disabled" aria-controls="dynamic-table" tabindex="0"><a href="#">&lt;</a></li>';


        $down_row           = $nowPage + 1; //下一页
        $down_page          = ($down_row <= $totalPages) ?
                                    '<li class="paginate_button " aria-controls="dynamic-table" tabindex="0"><a href="javascript:void(0)" hrefs="'.$url.'" page="'.$down_row.'">&gt;</a></li>' :
                                    '<li class="paginate_button disabled" aria-controls="dynamic-table" tabindex="0"><a href="#">&gt;</a></li>';


        $the_first           = '';//第一页

        if($totalPages > $rollPage && ($nowPage - $now_cool_page) >= 1){
            $the_first = '<li class="paginate_button previous" aria-controls="dynamic-table" tabindex="0" id="dynamic-table_previous">
                          <a href="javascript:void(0)" hrefs="'.$url.'" page="1"><i class="ace-icon fa fa-angle-double-left"></i></a></li>';
        }else if(!($nowPage - $now_cool_page) >= 1){
            $the_first = '<li class="paginate_button previous disabled" aria-controls="dynamic-table" tabindex="0" id="dynamic-table_previous">
                          <a href="#"><i class="ace-icon fa fa-angle-double-left"></i></a></li>';
        }

        //最后一页
        $the_end = '';
        if($totalPages > $rollPage && ($nowPage + $now_cool_page) < $totalPages){
            $the_end = '<li class="paginate_button next" aria-controls="dynamic-table" tabindex="0" id="dynamic-table_next">
                        <a href="javascript:void(0)" hrefs="'.$url.'" page="'.$totalPages.'"><i class="ace-icon fa fa-angle-double-right"></i></a></li>';
        }

        //数字连接
        $link_page = "";
        for($i = 1; $i <= $rollPage; $i++){
            if(($nowPage - $now_cool_page) <= 0 ){
                $page = $i;
            }elseif(($nowPage + $now_cool_page - 1) >= $totalPages){
                $page = $totalPages - $rollPage + $i;
            }else{
                $page = $nowPage - $now_cool_page_ceil + $i;
            }
            if($page > 0 && $page != $nowPage){

                if($page <= $totalPages){
                    $link_page .= '<li class="paginate_button " aria-controls="dynamic-table" tabindex="0"><a href="javascript:void(0)" hrefs="'.$url.'" page="'.$page.'">' . $page . '</a></li>';
                }else{
                    break;
                }
            }else{
                if($page > 0 && $totalPages != 1){
                    $link_page .= '<li class="paginate_button active" aria-controls="dynamic-table" tabindex="0"><a href="#">'.$page.'</a></li>';
                }
            }
        }
        return array($nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $totalRows, $totalPages,$datalist);


    }

}